#ifndef __IMA_SERVICE_H__
#define __IMA_SERVICE_H__ 

#include <ServiceSkeleton.h>
#include "ImaLogger.h"
#include "ImaConf.h"
#include "ImaJsonAssembler.h"

using namespace wso2wsf;
using namespace std;

ImaLogger* gpLogger = NULL;
ImaConf* gpConf = NULL;
bool gImaActiveUsersInstanceCreated = false;

class ImaService: public ServiceSkeleton {
	public:
			
		WSF_EXTERN WSF_CALL ImaService(){};
		OMElement* WSF_CALL invoke(OMElement *message, MessageContext *pMsgCtx);
		OMElement* WSF_CALL onFault(OMElement *ele);
		void WSF_CALL init();

		OMElement* greet(OMElement* pMsg);
		OMElement* userFeedback(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* registerUser(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* confirmUserRegistration(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* updateUserDetails(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* login(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* logout(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* changePassword(OMElement* inMsg, MessageContext* pMsgCtx);
		OMElement* forgotPassword(OMElement* inMsg, MessageContext* pMsgCtx);
		OMElement* fetchUserData(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* addInfo(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* updateInfo(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* deleteInfo(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* addRecipient(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* updateRecipient(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* deleteRecipient(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* addInfoRecipients(OMElement* pMsg, MessageContext* pMsgCtx);
//		OMElement* updateInfoRecipient(OMElement* pMsg, MessageContext* pMsgCtx);
		OMElement* deleteInfoRecipient(OMElement* pMsg, MessageContext* pMsgCtx);
//		OMElement* updateLastAccessTime(OMElement* pMsg, MessageContext* pMsgCtx);
	private:
		OMElement* createChildElement(OMElement*& pParent, const char* pname, const char* pText=NULL);
		OMElement* createChildElement(OMElement*& pParent, const char* pname, const long& value);
		OMAttribute* addAttribute(const char* pAttrib, const std::string& strAttribVal, OMElement* pEle);
		int extractMessagesFromJson(std::string& jsonString, std::vector<string>& messages);
		int getJsonStringValue(const std::string& jsonString, const char* pKey, std::string& value);
		int getJsonStringValue(const json_t* pRoot, const char* pKey, std::string& value);
		int getJsonIntValue(string& jsonString, const char* pKey, long& value);
		int getJsonIntValue(const json_t* pRoot, const char* pKey, long& value);
		std::string getElementText(OMElement* pElement);
		int extractMessagesFromJsonAndAppendIntoXmlEle(string& jsonString, OMElement*& pMessagesEle);
		int extractInfoRecipients(OMElement* pRecipientsEle, std::vector<INFO_RECIPIENT*>& recpientVector);
		int extractInfoRecipients(const json_t* pRecipientIdListJsonObj, std::vector<unsigned long>& recipientIdList);
		bool getAttributeValue(const char* pAttributeName, std::string& strAttribVal);
		bool getRecipientDetails(OMElement* pRecipientEle, INFO_RECIPIENT*& pRecipient);
		bool assembleAddInfoRecipientsXmlResponse(const std::string& strJsonRespPkt, std::vector<INFO_RECIPIENT*>& recipientsVector, OMElement* pRespElement);
		int extractRecipientDetails(OMElement* pDetailsEle, std::string& strName, std::string& strEmailId, std::string& strPhone, std::string& strAddress, std::string& strRelationship, OMElement* pMessagesElement);
		bool assembleFetchUserDataXmlResponse(const std::string& strJsonStr, OMElement* pRespElement, std::string& errMsgIfAny);
		bool assembleUserDetailsObject(const json_t* pUserDetailsJsonObj, OMElement* pUserDetailsEle);
		bool assembleRecipientListObject(const json_t* pRecipientListJsonObj, OMElement* pRecipientListEle);
		bool assembleInfoListObject(const json_t* pInfoListJsonObj, OMElement* pInfoListEle);
		int extractRecipientDetails(const json_t* pRecipientObject, unsigned long& recipientId, const char*& pRecipientName, const char*& pRecipientEmail, const char*& pRecipientPhone, const char*& pRecipientAddress, const char*& pRecipientRelationWithOwner);
		int extractInfoDetails(const json_t* pInfoObject, unsigned long& infoId, const char*& pInfoText, unsigned short& media, unsigned long& ttl, std::vector<unsigned long>& recipientIdList);
		int extractRecipientIds(OMElement* pElement, std::vector<unsigned long>& recipientIds);
		int fetchUserData(std::string& strSource, std::string& strEmailId, OMElement*& pRespEle);
};

#endif // __IMA_SERVICE_H__
